﻿using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using Server;
using System;
using System.Text;
using System.Threading;

namespace WorkConsumer1
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("接收到消息");
            WorkConsumer.ReciveMsg();
            Console.ReadLine();
        }
    }
    class WorkConsumer
    {
        private static readonly ConnectionFactory rabbitMqFactory = CommonConnectionFactory.rabbitMqFactory;

        public static void ReciveMsg()
        {

            using (IConnection conn = rabbitMqFactory.CreateConnection())
            {
                using (IModel channel = conn.CreateModel())
                {
                    string QueueName = "工作队列";
                    channel.QueueDeclare(QueueName, durable: true, autoDelete: false, exclusive: false, arguments: null);
                    // channel.QueueBind(QueueName, "交换机", routingKey: "工作模式");
                    channel.BasicQos(0, 1, false);
                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += (model, ea) =>
                    {
                        Thread.Sleep(1000);
                        var msgBody = Encoding.UTF8.GetString(ea.Body.ToArray());
                        Console.WriteLine($@"[{msgBody}] OK");


                        channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
                    };
                    channel.BasicConsume(QueueName, false, consumer: consumer);
                    Console.WriteLine("接收到消息");
                    Console.ReadLine();
                }
            }
        }
    }
}
